//
//  TAMTReportController.swift
//  TAMeetApp
//
//  Created by Admin on 2024/12/4.
//

import UIKit

class TAMTReportController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    
    let dataSource:[String] = ["头像、资料造假", "色情低俗", "政治敏感", "垃圾广告", "涉嫌欺诈", "其他"]
    
    
    private lazy var tableView: UITableView = {
        let apple = UITableView(frame: self.view.bounds, style: .plain)
        apple.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        apple.contentInsetAdjustmentBehavior = .never
        apple.estimatedRowHeight = 0
        apple.rowHeight = 55
        apple.delegate = self
        apple.dataSource = self
        apple.separatorStyle = .none
        apple.tableFooterView = UIView(frame: .zero)
        apple.showsVerticalScrollIndicator = false
        apple.showsHorizontalScrollIndicator = false
        apple.backgroundColor = UIColor.white
        apple.register(UITableViewCell.self, forCellReuseIdentifier: "cell1")
        return apple
    }()
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        self.navigationController?.setNavigationBarHidden(false, animated: true)
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        self.navigationItem.title = "举报理由"
        view.backgroundColor = .white
        
        view.addSubview(tableView)
        tableView.snp.makeConstraints { make in
            make.top.equalTo(view.safeAreaLayoutGuide.snp.top)
            make.bottom.equalTo(view.safeAreaLayoutGuide.snp.bottom)
            make.leading.trailing.equalToSuperview()
        }
    }
    
    // 返回表格的行数
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataSource.count
    }
    
    // 返回表格单元格的内容
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell1", for: indexPath) as UITableViewCell
        cell.selectionStyle = .none
        cell.textLabel?.text = dataSource[indexPath.row]
        cell.accessoryType = .disclosureIndicator
        return cell
    }
    
    // MARK: - UITableViewDelegate
    
    // 选中某一行时的响应方法
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        // 可以进行跳转等操作
        tableView.deselectRow(at: indexPath, animated: true)  // 取消选中状态
        let app = TAMTReportSubmitController()
        app.navigationItem.title = dataSource[indexPath.row]
        self.navigationController?.pushViewController(app, animated: true)
    }

}


class TAMTReportSubmitController: UIViewController, UITextViewDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    
    var reportContentTextView: UITextView!
    var imageView: UIImageView!
    var selectImageButton: UIButton!
    var submitButton: UIButton!
    var selectedImage: UIImage?

    override func viewDidLoad() {
        super.viewDidLoad()
        
        // 设置界面
        self.view.backgroundColor = .white

        setupUI()
    }
    
    func setupUI() {
        // 举报内容文本框
        reportContentTextView = UITextView()
        reportContentTextView.delegate = self
        reportContentTextView.font = UIFont.systemFont(ofSize: 16)
        reportContentTextView.layer.borderWidth = 1
        reportContentTextView.layer.borderColor = UIColor.gray.cgColor
        reportContentTextView.layer.cornerRadius = 5
//        reportContentTextView.placeholder = "请输入举报内容..."
        self.view.addSubview(reportContentTextView)
        
        reportContentTextView.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
            reportContentTextView.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.topAnchor, constant: 20),
            reportContentTextView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor, constant: 20),
            reportContentTextView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor, constant: -20),
            reportContentTextView.heightAnchor.constraint(equalToConstant: 150)
        ])
        
        // 上传图片按钮
        selectImageButton = UIButton(type: .system)
        selectImageButton.setTitle("选择截图/图片", for: .normal)
        selectImageButton.addTarget(self, action: #selector(selectImage), for: .touchUpInside)
        self.view.addSubview(selectImageButton)
        
        selectImageButton.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
            selectImageButton.topAnchor.constraint(equalTo: reportContentTextView.bottomAnchor, constant: 20),
            selectImageButton.centerXAnchor.constraint(equalTo: self.view.centerXAnchor)
        ])
        
        // 上传图片显示
        imageView = UIImageView()
        imageView.contentMode = .scaleAspectFit
        imageView.layer.borderWidth = 1
        imageView.layer.borderColor = UIColor.gray.cgColor
        imageView.layer.cornerRadius = 5
        imageView.isHidden = true
        self.view.addSubview(imageView)
        
        imageView.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
            imageView.topAnchor.constraint(equalTo: selectImageButton.bottomAnchor, constant: 20),
            imageView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor, constant: 20),
            imageView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor, constant: -20),
            imageView.heightAnchor.constraint(equalToConstant: 200)
        ])
        
        // 提交按钮
        submitButton = UIButton(type: .system)
        submitButton.setTitle("提交举报", for: .normal)
        submitButton.addTarget(self, action: #selector(submitReport), for: .touchUpInside)
        self.view.addSubview(submitButton)
        
        submitButton.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
            submitButton.bottomAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.bottomAnchor, constant: -20),
            submitButton.centerXAnchor.constraint(equalTo: self.view.centerXAnchor)
        ])
    }
    
    // MARK: - Select Image
    @objc func selectImage() {
        let imagePicker = UIImagePickerController()
        imagePicker.delegate = self
        imagePicker.sourceType = .photoLibrary
        imagePicker.allowsEditing = false
        present(imagePicker, animated: true, completion: nil)
    }

    // UIImagePickerControllerDelegate 方法
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        if let selectedImage = info[.originalImage] as? UIImage {
            self.selectedImage = selectedImage
            imageView.image = selectedImage
            imageView.isHidden = false
        }
        picker.dismiss(animated: true, completion: nil)
    }

    // 取消选择图片
    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        picker.dismiss(animated: true, completion: nil)
    }
    
    // MARK: - Submit Report
    @objc func submitReport() {
        guard let reportContent = reportContentTextView.text, !reportContent.isEmpty else {
            showAlert(message: "请输入举报内容")
            return
        }
        
        // 在这里处理上传报告和图片的逻辑（上传到服务器等）
        // 例如：
        // reportContent 和 selectedImage 上传到服务器
        
        showAlert(message: "举报已提交")
    }

    // 显示提示框
    func showAlert(message: String) {
        let alert = UIAlertController(title: nil, message: message, preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "确定", style: .default, handler: nil))
        present(alert, animated: true, completion: nil)
    }
}

